package com.ruoyi.project.business.domain.entity;

import com.alibaba.fastjson.JSON;
import com.ruoyi.project.business.domain.model.HardSoftScore;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.daily.DailyScheduleSolution;
import com.ruoyi.project.business.domain.model.daily.Post;
import com.ruoyi.project.business.domain.model.daily.TimePeriodMirror;
import com.ruoyi.project.business.domain.rules.Rule;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

@Data
@Slf4j
public class DailyScheduleEmp extends Emp{

    /**
     * 员工技能, 初始化进来的
     */
    private List<Post> empPosts;

    // 运行时参数
    private TimePeriodMirror tryAssignTimePeriod;


    public JudgeResult ifAssign(DailyScheduleSolution solution) {
        log.info("emp: {} 能不能匹配： timePeriod: {}", this.getUserId(), JSON.toJSONString(tryAssignTimePeriod));

        List<Rule> empWanted = getRules();
        JudgeResult result = new JudgeResult();
        if (CollectionUtils.isEmpty(empWanted)) {
            result.setResult(Rule.RESULT_ACCEPT);
            result.mergeScore(HardSoftScore.defaultScore());
        } else {
            for (Rule rule : empWanted) {
                JudgeResult judge = rule.judge(solution, this);
                if (judge.isAccept()) {
                    // 接受并继续
                    result.setTips(rule.getClass().getSimpleName() + "接受并继续");
                    result.mergeScore(judge.getScore());
                    result.setTimePeriod(tryAssignTimePeriod);
                    result.setResult(Rule.RESULT_ACCEPT);
                } else if (judge.isStopTask()) {
                    // 终止任务
                    result.setTips(judge.getTips());
                    result.setResult(Rule.STOP_TASK);
                    break;
                } else if (judge.isStopRule()) {
                    // 拒绝但继续
                    result.setTips(judge.getTips());
                    result.mergeScore(judge.getScore());
                    result.setResult(Rule.REJECT_AND_CONTINUE_TASK);
                    break;
                } else {

                }
            }
        }

        return result;
    }
}
